Ext.onReady(function() {
    var panel=  new Ext.panel.Panel ({
    	renderTo:Ext.getBody(),
    	height:'100%',
        width:'100%',
	    layout: 'border',
        defaults: {
            split: true,                  //是否有分割线
            collapsible: true           //是否可以折叠
        },
        dockedItems: [{
	          xtype: 'toolbar',
	          dock: 'top',//bottom
	          items: buttons
			}],
        border: false,
        items: [initLeftTree(),initPanel(),initRightConfig()]
	});
});

function initLeftTree(){
	var panel = new Ext.panel.Panel({
        region:'west',
        xtype: 'panel',
        title: '报表',
        width: 250,
        layout: 'fit',
        items:[initTree()]
	});
	return panel;
}


function initTree(){
	var searchTbar=[//搜索区域
	          {
	              xtype: 'textfield',
	              width: '80%',
	              name: 'search_text',
	              emptyText: '请输入关键字检索',
	              enableKeyEvents: true,
	              listeners: {
	                  keypress: function (self, e) {
	                      if (e.getKey() == Ext.EventObject.ENTER) {
	                          treeSearch();
	                      }
	                  }
	              }
	          },
	          {
	              xtype: 'button',
	              text: '搜索',
	              handler: function (self) {
	                  treeSearch();
	              }
	          }
	      ]
    var tree=  new Ext.panel.Panel ({
    	layout: {
            type: 'vbox',
            align: 'stretch',
            flex: 1
        },
        height: '100%',
        border: true,
	    items:
	    [{
            width: '100%',
            flex: 5,
            xtype: 'treepanel',
            store: reportStroe,
            rootVisible: true,
            tbar:searchTbar,
            itemId: 'manageTree',
            listeners: {
            	afterlayout: function (self) {
					if(q_id==null){
						return ;
					}
            		var node = self.getStore().getNodeById(q_id);
					if(node==null){
						return ;
					}
					currentReport = node;
            		self.getSelectionModel().select(node);
    	    	},
                itemclick: function (view, record, item, index, e, eOpts) {
                	if(!record.get("leaf")){
                		return;
                	}
                	currentReport = record;
                	if(changeFlag){
                		Ext.Msg.confirm('提示', q_title+'报表已修改是否保存修改的报表', function (btn_confirm) {
			                if (btn_confirm == 'yes') {
			                	saveReport();
//			                	changeReport(record);
			                }else{
			                	changeReport(record);
			                }
			            }); 
                	}else{
                		changeReport(record);
                	}
                },
                'itemcontextmenu': function (view, record, item, index, e, eOpts) {
                	//禁用浏览器的右键相应事件
                    e.preventDefault();
                    e.stopEvent();
                }
            }
        },{
            width: '100%',
            flex: 5,
            xtype: 'treepanel',
            itemId: 'fieldTree',
            store: fieldStore,
            rootVisible: false,
            header:{
		    	xtype:'header',
		    	title:'字段',
		    	titleAlign:'left',
		    	height: 35,
		    	margin:'0 0 0 0',
	 	 		padding:'0 0 0 5',
	 	 		items:[{
	 	 			xtype: "label", 
	 	 			margin:'0 180 0 0',
	 	 			html:'<img src="/report/image/sysbutton/refresh_white.png" width="18px"; title="刷新" onclick ="refreshFieldTree()" />'
	 	 		}]
	 	 	},
            listeners: {
                'itemcontextmenu': function (view, record, item, index, e, eOpts) {
                    //禁用浏览器的右键相应事件
                    e.preventDefault();
                    e.stopEvent();
                    currentNode = record;
                    fieldRightClick.showAt(e.getXY());
                },
                'itemmouseenter':function (view , record , item , index , e , eOpts) {
            		if(view.tip==undefined || view.tip == null){
            			view.tip = Ext.create('Ext.tip.ToolTip', {  
                			        target: view.el,  //或者item
                			        delegate: view.itemSelector,  
                			        trackMouse: true,  
                			        renderTo: Ext.getBody(),  
                					itemLevel:1,
                					html:'右击添加条件'
                			    }); 
            		}else{
            				view.tip.update('右击添加条件'); 
            		}
            	},
            	'itemmousedown': function ( self, record, item, index, e, el ) {
            		dropValue = record.data;
            		var drag = document.getElementById("dragDiv");
            		if(drag){
            			drag.innerHTML = '<image src="/report/image/sysbutton/jinzhi.png" style="width:16px;height:16px;margin:2px 2px 0 2px;"/>'+dropValue.text;
            			$("#dragDiv").css('display','block');
            		}else{
            			var dragDiv = document.createElement('div');
                		dragDiv.innerHTML = '<image src="/report/image/sysbutton/jinzhi.png" style="width:16px;height:16px;margin:2px 2px 0 2px;"/>'+dropValue.text;
                		dragDiv.style.display = "none";
                		dragDiv.style.cssText = "position:absolute;height:22px;border:1px solid #C0C0C0;line-height:22px;vertical-align:middle;font-size:10px;"
                		dragDiv.id = "dragDiv";
            		    //Odiv.className="Box";                    //div的class为Box
            		    document.body.appendChild(dragDiv);        //在body内创建一个div
            		    drag = document.getElementById("dragDiv");
            		}
            		$(document).bind('mousemove', function(move_e){
            			if(dropValue){
            				drag.style.left = move_e.screenX+10 + "px";
                			drag.style.top = move_e.screenY-70 +  "px";
            			}
            		}).bind('mouseup', function(up_e){
            			dropValue = null;
            			$("#dragDiv").css('display','none');
            		});
            	},
            }
	      }]
	});
    return tree;
}



/**
 * 初始化右侧panel，放置条件和grid
 */
function initPanel() {
    return Ext.create('Ext.panel.Panel', {
    	itemId:'mainPanel',
        height: '100%',
        flex: 5,
        region: 'center',
        collapsible: false,
        layout: {
            type: 'vbox',
            align: 'stretch'
        },
        border: false,
        items: [initSelect(),initGrid()],
    });
}


/**
 * 初始化右侧查询条件
 */
function initSelect() {
	var formPanel = new Ext.form.Panel({
	    width:'100%',
	    itemId:'condition',
	 	autoScroll: true, 
	 	layout:'column', 
	 	flex:4,
	    border: false,
	    frame: false,
	    title:q_title,
	    defaults:{
	    	labelAlign: "right",
	        xtype:"textfield",
	        labelWidth:110,
		    flex: 1,
		    columnWidth:0.3,
	        margin:0
	     },
	   items: [],
	   listeners: {
		   afterlayout:function ( self, layout, eOpts ) {
			  /* var browserInfo = getBrowserInfo();
   		       if(browserInfo["browser"]=="IE"){
	   		    	self.body.el.dom.onmousemove = function(){
	   		    		if(!panelDragFlag){
		 					   return;
	   		    		}
	 				   var record = panelDragTemp;
	 				   addCondition(record);
	 				   panelDragTemp = {};
	 				   panelDragFlag=false;
	 			   }
   		       }else{
	   		    	self.body.el.dom.onmouseup = function(){
	 				   if(!panelDragFlag){
	 					   return;
	 				   }
	 				   var record = panelDragTemp;
	 				   addCondition(record);
	 				   panelDragTemp = {};
	 				   panelDragFlag=false;
	 			   } 
   		       }*/
			   var browserInfo = getBrowserInfo();
			   if(browserInfo["browser"]=="IE"){
				   self.body.el.dom.onmouseover = function(e){
					   if(dropValue){
			    		   var drag = document.getElementById("dragDiv");
			           		if(drag){
			           			var value = dropValue.text;
			           			drag.innerHTML = '<image src="/report/image/sysbutton/yunxu.png" style="width:16px;height:16px;margin:2px 2px 0 2px;"/>'+value;
			           			$("#dragDiv").css('display','block');
			           		}
			    	   }
				   }
				   self.body.el.dom.onmspointerup = function(e){
					   if(dropValue){
			    		    var drag = document.getElementById("dragDiv");
			           		if(drag){
			           			addCondition(dropValue);
			           			dropValue = null;
			           			$("#dragDiv").css('display','none');
			           		}
			    	   }
				   }
			   }else{
				   self.body.el.dom.onmousemove = function(e){
			    	   if(dropValue){
			    		   var drag = document.getElementById("dragDiv");
			           		if(drag){
			           			var value = dropValue.text;
			           			drag.innerHTML = '<image src="/report/image/sysbutton/yunxu.png" style="width:16px;height:16px;margin:2px 2px 0 2px;"/>'+value;
//			           			$("#dragDiv").css('display','block');
			           		}
			    	   }
			       }
			       self.body.el.dom.onmouseup = function(e){
			    	   if(dropValue){
			    		    var drag = document.getElementById("dragDiv");
			           		if(drag){
			           			addCondition(dropValue);
			           			dropValue = null;
			           			$("#dragDiv").css('display','none');
			           		}
			    	   }
			       }
			   }
			   
			   self.body.el.dom.onmouseout = function(e){
		    	   if(dropValue){
		    		   var drag = document.getElementById("dragDiv");
		           		if(drag){
		           			var value = dropValue.text;
		           			drag.innerHTML = '<image src="/report/image/sysbutton/jinzhi.png" style="width:16px;height:16px;margin:2px 2px 0 2px;"/>'+value;
		           		}
		    	   }
		       }
		   }
	   	}
    });
    return formPanel;
}



/**
 * 初始化右侧主表格
 */
function initGrid() {
	var gridPanel = new Ext.panel.Panel({
		flex: 8,
	    height: '100%',
	    html:'<iframe id="reportManageGridIframe" src="" style="width:100%;height:100%;margin:0px auto;" frameborder="0"></iframe>'
	});
    return gridPanel;
}

function initRightConfig(){
	var panel = new Ext.panel.Panel({
		region: 'east',     // center region is required, no width/height specified
        xtype: 'panel',
        layout: 'fit',
        width: 250,
        itemId: 'rightConfig',
        title:'',
        items:[]
	});
	return panel;
}
